from collections import defaultdict


def lights(s):  # !!!!YGRB
    # RBYG
    res = [0] * 4
    n = len(s)
    while '!' in s:
        for i in range(n-3):
            table = defaultdict(int)
            window = s[i:i+4]
            pos = -1  # position of !
            for j, c in enumerate(window):
                if c == '!':
                    pos = j + i  # 记录 abs pos
                table[c] += 1
            if table['!'] > 1:
                continue
            elif table['!'] == 1:
                for k, c in enumerate(['R', 'B', 'Y', 'G']):
                    if table[c] == 0:
                        res[k] += 1
                        s = s[:pos] + c + s[pos+1:]
                        # s[pos] = c  # 填入字符
    return res


if __name__ == "__main__":
    print(lights('B!GR'))
    print(lights('!!!!YGRB'))
    print(lights('YGRB!!!!!'))
    print(lights('YGRB!!!!YGRB!!'))
